#include <signal.h>

#ifndef NULL
#define NULL ((void*)0)
#endif

static void sig_alarm_handler(int sig) {

}

int main(void)
{
    sigset_t oldset, newset;
    struct sigaction oldact;

    sigemptyset(&newset);
    struct sigaction newact = {
        .sa_handler = sig_alarm_handler,
        .sa_sigaction = NULL,
        .sa_mask = newset,
        .sa_flags = 0
    };
    //临时屏蔽SIGALRM信号, 防止在挂起前闹钟超时ALARM信号被处理导致进程挂起后无法
    //唤醒
    sigaddset(&newset, SIGALRM);
    sigprocmask(SIG_BLOCK, &newset, &oldset);

    

    //注册空的ALARM信号处理函数，防止进程退出
    sigaction(SIGALRM, &newact, &oldact);
    

    //定时发送SIGALRM信号
    alarm(2);

    sigdelset(&newset, SIGALRM);

    //解除SIGALRM信号屏蔽同时挂起进程（原子操作）
    //等待进程被SIGALRM信号唤醒
    sigsuspend(&newset);

    //恢复进程信号屏蔽字和处理动作
    sigprocmask(SIG_SETMASK, &oldset, NULL); 
    sigaction(SIGALRM, &oldact, NULL);
   
    return 0;
  
}

